// Tags:
#include <algorithm>
#include <cctype>
#include <cstdio>

typedef long long ll;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

const int N = 5e5 + 5, mod = 1e9 + 7, inf = 0x3f3f3f3f;
int n, ans, a[N];

inline void add(int &x, int y) {
  if ((x += y) >= mod) x -= mod;
}

void solve(int l, int r) {
  if (l == r) return add(ans, (ll)a[l] * a[l] % mod);
  static int minl[N], maxl[N];
  static ll summin[N], summax[N], sumans[N];
  int mid = (l + r) >> 1;
  minl[mid + 1] = inf, maxl[mid + 1] = -inf,
             sumans[mid + 1] = summax[mid + 1] = summin[mid + 1] = 0;
  for (int i = mid; i >= l; --i) {
    minl[i] = std::min(minl[i + 1], a[i]);
    maxl[i] = std::max(maxl[i + 1], a[i]);
    summin[i] = (summin[i + 1] + minl[i]) % mod;
    summax[i] = (summax[i + 1] + maxl[i]) % mod;
    sumans[i] = (sumans[i + 1] + maxl[i] * minl[i] % mod) % mod;
  }

  for (int i = mid + 1, rmin = inf, rmax = -inf, j = mid, k = mid; i <= r;
       ++i) {
    rmax = std::max(a[i], rmax);
    rmin = std::min(a[i], rmin);
    while (j >= l && maxl[j] < rmax) --j;
    while (k >= l && minl[k] > rmin) --k;
    if (j > k)
      add(ans, ((ll)(mid - j) * rmax % mod * rmin % mod +
                rmin * (summax[k + 1] - summax[j + 1]) % mod + sumans[l] -
                sumans[k + 1]) %
                   mod);
    else
      add(ans, ((ll)(mid - k) * rmax % mod * rmin % mod +
                rmax * (summin[j + 1] - summin[k + 1]) % mod + sumans[l] -
                sumans[j + 1]) %
                   mod);
  }

  solve(l, mid);
  solve(mid + 1, r);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("seq.in", "r", stdin);
  freopen("seq.out", "w", stdout);
#endif
#endif

  read(n);
  for (int i = 1; i <= n; ++i) read(a[i]);
  solve(1, n);
  printf("%d", ans);
  return 0;
}